package com.tuxing.draa_data.service.subject;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tuxing.draa_data.dao.center.NewDcBibliographyPeriDao;
import com.tuxing.draa_data.dao.center.NewDcBibliographyPeriDetailDao;
import com.tuxing.draa_data.dao.center.NewDcPeriDao;
import com.tuxing.draa_data.pojo.center.NewDcBibliographyPeri;
import com.tuxing.draa_data.pojo.center.NewDcBibliographyPeriDetail;
import com.tuxing.draa_data.pojo.center.NewDcPeri;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yuanshihang
 * @version 1.0
 * @className AsyncService
 * @description 基础描述:
 * @date 2022/4/9 23:07
 */
//@EnableAsync
@Service
@Slf4j
public class AsyncService {

    @Resource
    private NewDcPeriDao newDcPeriDao;

    @Resource
    private NewDcBibliographyPeriDao newDcBibliographyPeriDao;

    @Resource
    private NewDcBibliographyPeriDetailDao newDcBibliographyPeriDetailDao;

    /**
     * @return 暂无返回数据
     * @descpription 进行超星电子资源期刊数据的更新和元数据保存
     * @author yuanshihang
     * @date 2022/4/8 11:09
     * @Version 1.0
     */
    @Async
    public void doChaoXingPeri(String threadName, Integer start, Integer end) {
        System.out.println(threadName + " 线程开始");
        String limitSql = "LIMIT " + start + ", " + end;
        QueryWrapper<NewDcPeri> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNull("PERI_RECORD_ID");
        queryWrapper.orderByAsc("MAG_ID");
        queryWrapper.last(limitSql);
        //海量数据查询，单次查询耗时，此处后续更改
        List<NewDcPeri> newDcPeriList = newDcPeriDao.selectList(queryWrapper);
        int i = 0;
        for (NewDcPeri newDcPeri : newDcPeriList) {
            QueryWrapper<NewDcBibliographyPeri> periQueryWrapper = new QueryWrapper<>();
            periQueryWrapper.like("MAG_ID", newDcPeri.getMagId());
            periQueryWrapper.last("LIMIT 1");
            NewDcBibliographyPeri peri = newDcBibliographyPeriDao.selectOne(periQueryWrapper);

//            //已比对上的数据进行更新
            if (peri != null) {
                newDcPeri.setPeriRecordId(peri.getRecordId());
                newDcPeriDao.updateById(newDcPeri);
//                log.info("magid更新成功" + newDcPeri.getMagId());
            } else {
                //未比对上的期刊先在刊种表中再次 查询有没有重复，如果没有就进行新增
                peri = new NewDcBibliographyPeri();
                peri.setDataType(0);
                peri.createByDcPeri(newDcPeri);
                newDcBibliographyPeriDao.insert(peri);
                Long recordId = peri.getRecordId();

                NewDcBibliographyPeriDetail periDetail = new NewDcBibliographyPeriDetail();
//                periDetail.setImpactFactor(newDcPeri.getIfValue());
//                periDetail.setSubject(newDcPeri.getSubjectName());
                periDetail.setRecordId(recordId);
                newDcBibliographyPeriDetailDao.insert(periDetail);

                newDcPeri.setPeriRecordId(recordId);
                newDcPeriDao.updateById(newDcPeri);
                log.info("新增期刊：" + newDcPeri.toString());
            }
            i++;
            System.out.println("线程：" + threadName + "已处理数据：" + i + "条");
        }
    }
}
